const app = getApp()
const connector = require('../../../utils/connector.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    arr:[],
    authorizationGet:"fasle",//个人信息不全提示
    gif:"false",//加载动画
  },
  //选择地区
  option(e){
    app.addressId = e.currentTarget.dataset.id
    this.setData({gif:"true"})
    vipUserGet(this)
    // wx.navigateTo({
    //   // url: '/pages/shopping/setMeal/setMeal',
    //   url: '../schoolVip/schoolVip',
    // })
  },

  //立即理赔
  settlement(e){
    let that = this
    if(app.globalData.identity){
      connector.showToasts("对不起，您不是学员，不能申请理赔")
    }else{
      let userIfo = wx.getStorageSync("userInfo")
      // console.log(userIfo)
      playIs(that,userIfo)
    }
   
    // wx.navigateTo({
    //   url: '/pages/shopping/subject/notice',
    // })
  },
  //跳转协议
  xyGo(e){
    wx.navigateTo({
      url: '/pages/shopping/agreement/agreement',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // console.log(wx.getStorageSync("userInfo").address_id)
    // vipList(this,wx.getStorageSync("userInfo").address_id)
    // vipUserGet(this)
    vipList(this)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {
    var that = this
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新

  },
})

//判断用户是否购买保险
function playIs(that,userIfo){
  console.log(userIfo)
  // let isPlay = userIfo.vip_status
  app.isPlay = userIfo.vip_status
  console.log(app.isPlay)
  // app.isPlay = 1

  // isPlay = 1 
  if(app.isPlay != 0){
    wx.navigateTo({
      url: '/pages/shopping/subject/notice',
    })
  }else{
    connector.showToasts("您还未购买vip服务，请先购买")
    // addressIs(that,userIfo)
  }
  
}
//获取用户信息
function vipUserGet(that){
  // console.log(wx.getStorageSync("users"))
  if(wx.getStorageSync("users") && wx.getStorageSync("users").realname && wx.getStorageSync("users").idcard && wx.getStorageSync("users").mail_address){
    // addressIs(that,userIfo)
    that.setData({gif:"false"})
    wx.navigateTo({
      url: '../schoolVip/schoolVip',
    })
  }else{
    connector.getFunc(connector.connector().vipUserGet, "", "get", app.tokens, function (res) {
      // console.log(res.data)
      if (res.data.result.length < 1) {
      }else{
        // console.log(res.data)
        if(res.data.result[0].realname && res.data.result[0].mail_address){
          wx.setStorageSync("users",res.data.result[0])
          // addressIs(that,userIfo)
          that.setData({gif:"false"})
          wx.navigateTo({
            url: '../schoolVip/schoolVip',
          })
        }else{
          wx.showModal({
            content: '检测到您还没完善个人信息，是否去完善？',
            confirmText: "确认",
            cancelText: "取消",
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/oneself/profile/redact/redact',
                })
              } else {
                wx.switchTab({
                  url: '/pages/shopping/shopping',
                })
              }
            }
          });
        }
        
      }
    }, function (res) {
      wx.showToast({
        title: "内部错误",
        icon: 'none',
        duration: 1000
      })
    })
  }
}
//保险商品地区列表
function vipList(that){
  connector.getFunc(connector.connector().vipAddress, "", "get", app.tokens, function (res) {
    console.log(res)
    if (res.statusCode == 200) {
      // console.log(res.data)
      that.setData({
        arr:res.data,
      })
    }else{
      connector.showToasts(res.errMsg)
    }
  }, function (res) {
    connector.showToasts("内部错误")
  })
}